package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.dto.ResourceDto;
import com.sofwin.mapper.SysResourceMapper;
import com.sofwin.mapper.SysRoleMapper;
import com.sofwin.mapper.SysRoleResourceMapper;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleResource;
import com.sofwin.pojo.SysRoleResourceExample;
import com.sofwin.service.RoleService;
import com.sofwin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2023/5/17 15:49
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRoleMapper mapper;
    @Autowired
    private SysRoleResourceMapper roleResourceMapper;
    @Autowired
    private SysResourceMapper resourceMapper;
    @Override
    public boolean saveOrUpdateRole(SysRole role) {
        if(role.getId()==null){
            role.setGmtCreate(DateUtil.getNow());
            return mapper.insertSelective(role)==1?true:false;
        }
        role.setGmtModified(DateUtil.getNow());
        return mapper.updateByPrimaryKeySelective(role)==1?true:false;
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeById(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public SysRole queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysRole> quryByPage(SysRole role, Integer pageNumber, Integer pageSize) {


        PageInfo<SysRole> page = PageHelper.startPage(pageNumber, pageSize).doSelectPageInfo(() -> {
            SysRoleExample example = new SysRoleExample();
            SysRoleExample.Criteria criteria = example.createCriteria();
            if (!StringUtils.isEmpty(role.getRoleName())) {
                criteria.andRoleNameLike("%" + role.getRoleName() + "%");
            }
            if (role.getStatus() != null && role.getStatus() != -1) {
                criteria.andStatusEqualTo(role.getStatus());
            }
            example.setOrderByClause("sort asc");
            mapper.selectByExample(example);
        });
        return page;
    }

    @Override
    public boolean saveRoleResources(Integer[] resourceIds, Integer roleId) {
        // 先把roleId的所有权限删除，

        SysRoleResourceExample roleResourceExample = new SysRoleResourceExample();
        SysRoleResourceExample.Criteria criteria = roleResourceExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(roleResourceExample);
        int num=0;
        // 将resourceIds循环插入
        for(Integer resourceId:resourceIds){
            SysRoleResource roleResource = new SysRoleResource();
            roleResource.setResourceId(resourceId);
            roleResource.setRoleId(roleId);
            num+= roleResourceMapper.insertSelective(roleResource);
        }


        return num==resourceIds.length?true:false;
    }

    @Override
    public List<ResourceDto> queryResourcesByRoleId(Integer roleId) {

        return resourceMapper.selectResourcesByRoleId_(roleId);
    }

    @Override
    public Set<String> queryRolePermissions(Integer roleId) {
        return mapper.queryRolePermissions(roleId);
    }
}
